<@pp.dropOutputFile/>
<#include "../common/validation.ftl">

<#-- generate package name -->

<#-- iterate table list-->
<#list doc.tables as table>
<#assign module = doc.artifactid?replace(".", "/") + "/" + table.moduleid?uncap_first>
<#assign fileName = table.name?cap_first>
<#assign data_url = module + "/" + fileName>
<@pp.changeOutputFile name="/main/webapp/" + data_url + "/showitems_js.ftl"/>
<#noparse>
var ADD_CHECKER = null;
var EDT_CHECKER = null;
$(function(){
	$("#dataTable").datagrid({
		fit: true,
		nowrap: true,
		striped: true,
		title : </#noparse>"${fileName}"<#noparse>,
		url:"${base}/</#noparse>${data_url}/getPage.action",
		queryParams : array2map($("#search_form").serializeArray()),
		sortName: "obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>",
		sortOrder: "desc",
		remoteSort: false,
		idField:"obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>",
		singleSelect:true,
		checkOnSelect:false,
		selectOnCheck:false,
		pageSize : 20,
		pageList:[20, 50, 100],
		frozenColumns:[[
			{field:"ck",checkbox:true},
			{title:"ID",hidden:true, field:"obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>",width:40,sortable:true},
			{field:"edit",title:"操作",width:70,align:"center",
				formatter:function(value,row,index){
					var edit = '<a href="javascript:showEdit(' + row['obj.<#if table.idFieldName??>${table.idFieldName}<#else>id</#if>'] + ')" title="修改" class="button-edit"><span class="glyphicon glyphicon-pencil"></span></a>';
					return edit;
				}
			}
		]],
		columns:[[
			<#list table.fields as field>
			<#if field.name?lower_case != "id">
			{field:"obj.${field.name}", sortable : "true", title:"${field.label}"}<#if field_has_next>,</#if>
			</#if>
			</#list>
			<#noparse>
		]],
		pagination : true,
		rownumbers : true,
		toolbar : "#toolbar",
		onLoadError:function(){
		},
		onLoadSuccess : function(data){
			//$(".button-edit").linkbutton({plain:true}).tooltip();
		}
	});
	
	// Validators
	var options = {
		rules : </#noparse>${getRules(table)},
		message : ${getMessages(table)}<#noparse>,
		errorElement: "em",
		errorPlacement: function(error, element ) {
			// Add the `help-block` class to the error element
			error.addClass( "help-block" );
			error.insertAfter(element.parent("div"));
		},
		highlight: function ( element, errorClass, validClass ) {
			$(element).parents(".form-group").addClass("has-error").removeClass("has-success");
		},
		unhighlight: function (element, errorClass, validClass) {
			$(element).parents(".form-group").addClass("has-success").removeClass("has-error");
		}
	};
	ADD_CHECKER = $("#addForm").validate(options);
	EDT_CHECKER = $("#editForm").validate(options);
});
	
function showAdd(){
	$("#addForm")[0].reset();
	$("#addDialog").dialog("open");
}

function deleteItems(){
	var rows = $("#dataTable").datagrid("getChecked");
	if(rows.length > 0){
		$.messager.confirm("操作提示", "确定要删除记录？", function(r){
			if (r){
				var items = {};
				for(var i=0;i<rows.length;i++){
					items["items[" + i + "]"] = rows[i]["obj.id"];
				}
				$.post("${base}/</#noparse>${data_url}<#noparse>/deleteItems.action", 
					items, 
					function(data){
						if(data.success == 0){
							$("#dataTable").datagrid("reload");
						}
						showTip(data.success == 0, data.message);				
					},
					"json"
				);
			}else{
			}
		});
	}
}

function add(){
	if(ADD_CHECKER.form()){
		$.post("${base}/</#noparse>${data_url}<#noparse>/create.action", 
			array2map($("#addForm").serializeArray()), 
			function(data){
				if(data.success == 0){
					$("#dataTable").datagrid("reload");
				}
				showTip(data.success == 0, data.message);				
			},
			"json"
		);
	}
}

function showEdit(id){
	loading("加载数据...");
	$.post("${base}/</#noparse>${data_url}<#noparse>/get.action", 
		{id : id}, 
		function(data){
			loaded();
			try{
				$("#editForm")[0].reset();
				$("#editForm").form("load", data.rows[0]);
				$("#editDialog").dialog("open");
			}catch(e){
				showTip(data.success == 0, "加载ID?" + id + "数据失败!(" + e + ")");
			}					
		},
		"json"
	);
}

function update(){
	if(EDT_CHECKER.form()){
		$.post("${base}/</#noparse>${data_url}<#noparse>/update.action", 
			array2map($("#editForm").serializeArray()), 
			function(data){
				if(data.success == 0){
					$("#dataTable").datagrid("reload");
				}
				showTip(data.success == 0, data.message);				
			},
			"json"
		);
	}
}

function search(){
	$('#dataTable').datagrid('load', array2map($("#search_form").serializeArray()));
}

function doImport(){
}

function doExport(){
	$.download("${base}/</#noparse>${data_url}<#noparse>/export.action", array2map($("#search_form").serializeArray()), "post");
}
</#noparse>
</#list>